package com.wuliupai.delivery.wuliupaidelivery.activity;

import android.os.Handler;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.wuliupai.delivery.wuliupaidelivery.App;
import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.bean.BaseBean;
import com.wuliupai.delivery.wuliupaidelivery.bean.LoginBean;
import com.wuliupai.delivery.wuliupaidelivery.bean.PwdBean;
import com.wuliupai.delivery.wuliupaidelivery.net.NetResultOperate;
import com.wuliupai.delivery.wuliupaidelivery.utils.JsonUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.LogUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.MD5;
import com.wuliupai.delivery.wuliupaidelivery.utils.StringUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.TimeCountDownUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

/**
 * 找回密码界面
 */
@EActivity(R.layout.activity_retrieve_password)
public class RetrievePasswordActivity extends AppBaseActivity {

    String phone;
    String valiCode;

//    @Extra("whichRetrievePwd")
//    String whichRetrievePwd;

    @ViewById(R.id.btn_retrieve_password_get_verification_code)
    Button btn_retrieve_password_get_verification_code; // 点击获取验证码

    @ViewById(R.id.tv_retrieve_password_phont_num)
    TextView tv_retrieve_password_phont_num; //

    @ViewById(R.id.et_my_wallet_password_set_new_pwd)
    EditText et_my_wallet_password_set_new_pwd; //重置密码输入

    @ViewById(R.id.et_retrieve_password_verification_code)
    EditText et_retrieve_password_verification_code; //重置密码验证码

    @ViewById(R.id.tv_loginVoiceCode)
    TextView tv_loginVoiceCode; //获取语言验证码

    private String mAes;
    private String mAes1;

    @Click(R.id.btn_retrieve_password_next)
    void onClikcRetrievePassword(){ // 点击提交
        //checkvalidationcoderesetpassword
        String verificationCode = et_retrieve_password_verification_code.getText().toString();
        String newPwd = et_my_wallet_password_set_new_pwd.getText().toString();

        if (!StringUtils.isEmpty(verificationCode)){
            if (!StringUtils.isEmpty(newPwd)){

                PwdBean bean = new PwdBean();
                bean.setValiCode(verificationCode);
                String newPwdmd5 = MD5.GetMD5Code(newPwd);
                bean.setNewPayPassword(newPwdmd5);
                String payJson = JsonUtils.createJsonString(bean);
                LogUtils.log("resetpaypassword= "+ payJson);
                try {
                    mAes1 = JsonUtils.aes(payJson);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                callNet(this, "resetpaypassword", mAes1, new NetResultOperate() {
                    @Override
                    public void onSuccess(String content) {
                        App.toast("重置支付密码成功");
                        finish();
                    }
                    @Override
                    public void onDataEmpty(String content) {}
                    @Override
                    public void onDataEnd(String content) {}
                });

            }else {
                App.toast("请设置新密码");
            }
        }else {
            App.toast("请输入验证码");
        }

    }

    @Click(R.id.btn_retrieve_password_get_verification_code)
    void getPwdTestCode() { //点击获取验证码
        getPwdTestCode("0");
    }

    @Click(R.id.tv_loginVoiceCode)
    void getVoicePwdTestCode(){ //点击获取语音验证码
        getPwdTestCode("1");
    }


    @AfterViews
    void onPageStart() {
        BaseBean baseBean = new BaseBean();
        phone = baseBean.getLoginName();
        setDefaultTitle(this, R.string.string_my_retrieve_password_title);
        tv_retrieve_password_phont_num.setText(phone);
        tv_loginVoiceCode.setText(Html.fromHtml("<font color='#999999'>收不到验证码？请使用</font><font color='#04b00f'>语音验证码</font>"));
    }

    /**
     * 获取验证码
     *
     * @param type 获取类型;0:短信验证码；1：语音验证码
     */
    private void getPwdTestCode(String type) {
        LoginBean resetPwd = new LoginBean();
        resetPwd.setLoginName(phone);
        resetPwd.setUserId(new BaseBean().getUserId());
        resetPwd.setToken(new BaseBean().getToken());
        resetPwd.setType(type);
        resetPwd.setVersion("1.0");
        String jsonPwdTestCode = JsonUtils.createJsonString(resetPwd);
        LogUtils.log("获取验证码send---" + jsonPwdTestCode);
        try {
            String aesPwdTestCode = JsonUtils.aes(jsonPwdTestCode);
            callNet(RetrievePasswordActivity.this, "getvalidationcodersppwd", aesPwdTestCode, new NetResultOperate() {
                @Override
                public void onSuccess(String content) {
                    LogUtils.log("获取验证码back---" + content);
                   /* if (content != null) {*/
                       /* LoginBean resetPwdParse = (LoginBean) JsonUtils.parseJsonString(content, LoginBean.class);
                        valiCode = resetPwdParse.getValiCode();*/

                        if (type.equals("0")) {
                            final Handler handler = new Handler();
                            Runnable runnable = new Runnable() {
                                @Override
                                public void run() {
                                    LogUtils.log("登录handler" + "run");
                                    tv_loginVoiceCode.setVisibility(View.VISIBLE);
                                    handler.postDelayed(this, 44000);
                                }
                            };
                            handler.postDelayed(runnable, 44000);
                            initCountDown();
                        }else{
                            App.toast("语音验证码发送成功");
                        }
                    //}
                }

                @Override
                public void onDataEmpty(String content) {

                }

                @Override
                public void onDataEnd(String content) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /* 初始化倒计时 */
    private void initCountDown() {
        TimeCountDownUtil timeCountDown = new TimeCountDownUtil(this,89999, 1000,
                btn_retrieve_password_get_verification_code);
        timeCountDown.start();
    }

}
